﻿using Liang.Util;
using Liang.Util.Task;
using Liang.Util.Task.Enums;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;


namespace LiangNetService
{
    public partial class MService : ServiceBase
    {
        private TaskService service;

        public MService()
        {
            InitializeComponent();
            this.ServiceName = "MarketingService";
            CanPauseAndContinue = true;
            CanStop = true;
            CanShutdown = true;
            CanHandleSessionChangeEvent = true;
            service = new TaskService();
        }

        public void TaskFunction()
        {
            LogHelper.WriteLog(typeof(MService), "出现异常！", 2);
        }

        protected override void OnStart(string[] args)
        {
//#if DEBUG
//            Debugger.Launch();    //Launches and attaches a debugger to the process.
//#endif
            List<TimeSpan> list = new List<TimeSpan>();
            TimeSpan start = new TimeSpan(0);
            TimeSpan end = new TimeSpan(23, 59, 59);
            list.Add(start);
            while (true)
            {
                start = start + new TimeSpan(0, 1, 0);
                list.Add(start);
                if (start >= end) break;
            }
            //service.Start(new TimerStrategy
            //{
            //    TimerMode = TimerStrategyMode.EveryDay,
            //    StrategyByDay = new TimerStrategyByDay
            //    {
            //        TimePoints = list.ToArray()
            //    }
            //}, null);
            /// 定时说明
            /// 只要调用StartSchedule，任务就自动按照定时规则开始定时
            /// 定时策略为： 每天，在设定的TimeSpan时间点上都执行一次，而且是可重入的执行，Reetrant为可重入开关
            TaskSchedule.StartSchedule("TaskFunction", new TaskService(),
                    new TimerStrategy
                    {
                        TimerMode = TimerStrategyMode.EveryDay,
                        Reentrant = true,
                        StrategyByDay = new TimerStrategyByDay
                        {
                            TimePoints = list.ToArray()
                        }
                    });
        }

        protected override void OnStop()
        {
           service.Stop(); 
        }
    }
}
